//
//  HMUserAccount.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/22.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMUserAccount.h"

#define PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userAccount.archiver"]

@interface HMUserAccount ()<NSCoding>

@end

@implementation HMUserAccount
    
-(instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
    
+(instancetype)userAccountWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}
    
    
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{}
    
-(instancetype)initWithCoder:(NSCoder *)decoder{
    if (self = [super init]) {
        self.name = [decoder decodeObjectForKey:@"name"];
        self.access_token = [decoder decodeObjectForKey:@"access_token"];
        self.uid = [decoder decodeIntegerForKey:@"uid"];
        self.expires_in = [decoder decodeIntegerForKey:@"expires_in"];
        self.profile_image_url = [decoder decodeObjectForKey:@"profile_image_url"];
    }
    return self;
}
    
-(void)encodeWithCoder:(NSCoder *)encoder{
//    encoder encode(access_token, forKey: "access_token")
//    encoder.encode(expiresDate, forKey: "expiresDate")
//    encoder.encode(uid, forKey: "uid")
//    encoder.encode(name, forKey: "name")
//    encoder.encode(avatar_large, forKey: "avatar_large")
    [encoder encodeObject:_access_token forKey:@"access_token"];
//    [encoder encodeObject:_expiresDate forKey:@"expiresDate"];
    [encoder encodeObject:_name forKey:@"name"];
    [encoder encodeInt:_uid forKey:@"uid"];
    [encoder encodeInt:_expires_in forKey:@"expires_in"];
    [encoder encodeObject:_profile_image_url forKey:@"profile_image_url"];
    
}
    
-(void)saveUserAccount{
//    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userAccount.archiver"];
    
    [NSKeyedArchiver archiveRootObject:self toFile:PATH];
}

+(instancetype)loadUserAccount{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
}
@end
